export default{
	checkLogin(){
		var userInfo=uni.getStorageSync('userInfo');
		if(!userInfo){
			uni.navigateTo({
				url:'/pages/log/log.vue'
			})
		}else{
			return userInfo;
		}
	},
	isJSON(str) {
	    if (typeof str == 'string') {
	        try {
	            var obj=JSON.parse(str);
	            if(typeof obj == 'object' && obj ){
	                return true;
	            }else{
	                return false;
	            }
	
	        } catch(e) {
	            return false;
	        }
	    }
	},
	dateFormat:function(time) {
	    var date=new Date(time);
	    var year=date.getFullYear();
	    /* 在日期格式中，月份是从0开始的，因此要加0
	     * 使用三元表达式在小于10的前面加0，以达到格式统一  如 09:11:05
	     * */
	    var month= date.getMonth()+1<10 ? "0"+(date.getMonth()+1) : date.getMonth()+1;
	    var day=date.getDate()<10 ? "0"+date.getDate() : date.getDate();
	    var hours=date.getHours()<10 ? "0"+date.getHours() : date.getHours();
	    var minutes=date.getMinutes()<10 ? "0"+date.getMinutes() : date.getMinutes();
	    var seconds=date.getSeconds()<10 ? "0"+date.getSeconds() : date.getSeconds();
	    // 拼接
	    // return year+"-"+month+"-"+day+" "+hours+":"+minutes+":"+seconds;
		return year+"-"+month+"-"+day;
	},
    getBeforeDate(n){
	    var now = new Date();
	    var aftertime = new Date(n);
	    var year = now.getFullYear();
	    var mon= now.getMonth()+1;
	    var day= now.getDate();
	    var year_after = aftertime.getFullYear();
	    var mon_after = aftertime.getMonth()+1;
	    var day_after = aftertime.getDate();
	    var chs = 0;
	    //获取当月的天数
	    function DayNumOfMonth(Year,Month)
	    {
	        return 32 - new Date(Year,Month-1,32).getDate();
	    }
	    if(aftertime.getTime() - now.getTime() < 0){
	        var temp1 = day_after;
	        var temp2 = mon_after;
	        var temp3 = year_after;
	        day_after = day;
	        mon_after = mon;
	        year_after = year;
	        day = temp1;
	        mon = temp2;
	        year = temp3;
	    }
	    if(year == year_after){//不跨年
	        if(mon == mon_after){//不跨年不跨月
	            chs += day_after-day;
	        }else{//不跨年跨月
	            chs += DayNumOfMonth(year,mon)- day+1;//加上第一个不满的
	            for(var i=1;i< mon_after-mon;i++){
	                chs += DayNumOfMonth(year,mon+i);
	            }
	            chs += day_after-1;//加上
	        }    
	    }else{//存在跨年
	        chs += DayNumOfMonth(year,mon)- day+1;//加上开始年份不满的一个月
	        for(var m=1;m<12-mon;m++){
	            chs += DayNumOfMonth(year,mon+m);
	        }
	        for(var j=1;j < year_after-year;j++){
	            if((year+j)%400 == 0 || (year+j)%4 == 0 && (year+j)%100 != 0){
	                chs += 366;
	            }else{
	                chs += 365;
	            }
	        }
	        for(var n=1;n <= mon_after;n++){
	            chs += DayNumOfMonth(year_after,n);
	        }
	        chs += day_after-1;
	    }
	    if(aftertime.getTime() - now.getTime() < 0){
	        return chs;
	    }else{
	         return -chs;
	    }
	}

}